/* eslint-disable max-lines */
import * as BASE from '@/stores/definitions-base';
import { Input } from '@alifd/next';
import { t } from '@lingui/macro';
import React from 'react';

const {
  DATETIME_TEXT,
  DATE_TEXT,
  TIME_TEXT,
  EMPTY_TEXT,
  ENUMS: ENUMSBase,
  FieldsGrid: FieldGridBase,
  TitleGrid,
  column: columnBase,
  toSelectDataSource,
} = BASE;
export { DATETIME_TEXT, DATE_TEXT, EMPTY_TEXT, TIME_TEXT, TitleGrid, toSelectDataSource };

export function column(...args) {
  return columnBase(orderModel, ...args);
}

export function FieldsGrid(props) {
  return <FieldGridBase model={orderModel} {...props} />;
}

//----------------------------------------------------------------------
export const ENUMS = {
  ...ENUMSBase,
  states: {
    0: t`已取消`,
    1: t`待派车`,
    2: t`进行中`,
    3: t`已完成`,
  },
  ticketState: {
    0: t`未乘车`,
    1: t`已乘车`,
  },
};

export const MAGIC_NUMBERS = {
  // 任务类型  0-已取消 1-待派车 2-进行中 3-已完成
  TASK_STATE: {
    CANCEL: '0',
    WAITING: '1',
    START: '2',
    FINISH: '3',
  },
  // 司机状态 0-取消 1-已派车 2-司机已出发 3-到达上车点 5-行程已结束,
  DRIVER_STATE: {
    CANCEL: '0',
    SEND_CAR: '1',
    GOING: '2',
    PICKED: '3',
    FINISH: '5',
  },
};

export const orderModel = {
  orderBaseModel: {
    label: t`订单相关信息`,
    flightNumber: {
      label: t`航班号`,
    },
    orderType: {
      label: t`订单类型，0 - 点对点；1 - 时租`,
    },
    vehicleId: {
      label: t`车辆id`,
    },
    driverId: {
      label: t`司机id`,
    },
    driverName: {
      label: t`司机名`,
    },
    language: [
      {
        label: t`语言要求`,
      },
    ],
    startMainAreaText: {
      label: t`开始城市`,
    },
    endMainAreaText: {
      label: t`结束城市`,
    },
    portTypeText: {
      label: t`口岸`,
    },
    passengerName: {
      label: t`乘坐人`,
    },
    passengerPhone: {
      label: t`乘坐人电话`,
    },
    subscribeName: {
      label: t`预约人`,
    },
    subscribePhone: {
      label: t`预约人电话`,
    },
    number: {
      label: t`人数`,
    },
    numberOfLuggage: {
      label: t`行李数`,
    },
    startMainArea: {
      label: t`开始城市编号`,
    },
    startMinorArea: {
      label: t`开始区域编号`,
    },
    startStationName: {
      label: t`开始站点名称`,
    },
    endMainArea: {
      label: t`结束城市编号`,
    },
    endMinorArea: {
      label: t`结束区域编号`,
    },
    endStationName: {
      label: t`结束站点名称`,
    },
    predictBeginDate: {
      label: t`预估开始时间`,
      ui: {
        renderer: DATETIME_TEXT,
      },
    },
    predictEndDate: {
      label: t`预估结束时间`,
    },
    isPass: {
      label: t`是否过关`,
    },
    portType: {
      label: t`口岸类型，0 - 深圳湾口岸；1 - 澳门口岸；2 - 珠海口岸`,
    },
    isMap: {
      label: t`需要地图链接`,
    },
    predictTime: {
      label: t`预估时长`,
    },
    mileage: {
      label: t`里程`,
    },
    beginName: {
      label: t`开始地址`,
      ui: {
        span: 2,
      },
    },
    beginTude: {
      label: t`开始地址经纬度`,
    },
    endName: {
      label: t`结束地址`,
      ui: {
        span: 2,
      },
    },
    endTude: {
      label: t`结束地址经纬度`,
    },
    surcharges: [
      {
        label: t`附加费列表`,
      },
    ],
    roadFee: {
      label: t`路桥费`,
    },
    oilFee: {
      label: t`加油费`,
    },
    partFee: {
      label: t`停车费`,
    },
    repairFee: {
      label: t`维修费`,
    },
    otherFee: {
      label: t`其他费用`,
    },
    surchargeExplain: {
      label: t`附加费说明`,
    },
    addFee: {
      label: t`附加总费用`,
    },
    surchargePayStatus: {
      label: t`附加费支付状态，0 - 未支付；1 - 已支付；`,
    },
    surchargePayUrl: {
      label: t`附加费微信支付地址`,
    },
  },
  driverInfo: [
    {
      label: t`相关司机信息`,
      number: {
        label: t`工单id`,
      },
      licenseNo: {
        label: t`车牌编号`,
      },
      vehicle: {
        label: t`承运车辆`,
      },
      driverId: {
        label: t`司机id`,
      },
      name: {
        label: t`承运司机`,
        ui: {
          renderer: (v, r) => {
            const { driverInfo = [] } = r || {};
            let name = '无';
            for (const driver of driverInfo) {
              const { name: driverName, state } = driver;
              if (state !== '0') {
                name = driverName;
              }
            }
            return name;
          },
        },
      },
      phone: {
        label: t`司机电话`,
        ui: {
          renderer: (v, r) => {
            const { driverInfo = [] } = r || {};
            let phone = '无';
            for (const driver of driverInfo) {
              const { phone: driverPhone, state } = driver;
              if (state !== '0') {
                phone = driverPhone;
              }
            }
            return phone;
          },
        },
      },
      state: {
        label: t`司机状态 0-取消 1-已派车 2-司机已出发 3-接到乘客/开始上客 4-行程已开始 5-行程已结束`,
      },
      remark: {
        label: t`取消备注`,
      },
    },
  ],
  number: {
    label: t`任务编号 BCRW+yyyyMMdd+0001 ZXRW+yyyyMMdd+0001`,
  },
  orderIds: [
    {
      label: t`相关订单集合`,
    },
  ],
  state: {
    label: t`状态`,
    text: (v) => ENUMS.states[v],
    //  0-已取消 1-待派车 2-进行中 3-已完成`,
  },
  departureTime: {
    label: t`出发时间`,
    text: DATETIME_TEXT,
  },
  taskType: {
    label: t`任务类型 0：包车任务，1：专线任务`,
  },
  carrierCompany: {
    label: t`承运公司`,
  },
  dispatcher: {
    label: t`调度员`,
  },
  dispatcherName: {
    label: t`调度员名称`,
  },
  carrierCompanyName: {
    label: t`承运公司`,
  },
  carrierPrice: {
    label: t`公司价格`,
  },
  totalPrice: {
    label: t`总价`,
    text: (v) => v?.toFixed(2),
    ui: {
      renderer: (v) => v?.toFixed(2),
    },
  },
  negotiatedPrice: {
    label: t`协议价格`,
  },
  remark: {
    label: t`备注信息`,
    ui: {
      span: 4,
    },
    ui_edit: {
      renderer: (v) => <Input.TextArea value={v} />,
      span: 4,
    },
  },
  cancelRemark: {
    label: t`取消原因`,
    ui: {
      span: 4,
    },
    ui_edit: {
      renderer: (v) => <Input.TextArea value={v} />,
      span: 4,
    },
  },
  driverServiceFee: {
    label: t`司机服务费`,
  },
  midwayFee: {
    label: t`中途费用`,
  },
  driverDisbursement: {
    label: t`司机垫付费用`,
  },
  fleetSettlement: {
    label: t`公司结算状态`,
    text: (v) => (v ? t`已结算` : t`未结算`),
  },
  driverSettlement: {
    label: t`司机结算`,
    text: (v) => (v ? t`已结算` : t`未结算`),
  },
  orderSettlement: {
    label: t`下单人后付费用是否结算`,
  },
  cancelTime: {
    label: t`取消时间`,
    text: DATETIME_TEXT,
  },
  completionTime: {
    label: t`完成时间`,
  },
  companySettlementId: {
    label: t`公司结算id`,
  },
  driverSettlementId: {
    label: t`司机结算id`,
  },
  closingOfDistribution: {
    label: t`是否分账结束`,
  },
  direction: {
    label: t`false-第一段任务 true-第二段任务`,
  },
  tickets: [
    {
      label: t`车票信息`,
      orderId: {
        label: t`订单id`,
      },
      ticketCode: {
        label: t`车票号`,
      },
      ticketPrice: {
        label: t`票价`,
      },
      shift: {
        label: t`班次id`,
      },
      fromStation: {
        label: t`上车站点`,
      },
      fromStationName: {
        label: t`上车站点名称`,
      },
      fromStationAddress: {
        label: t`详情地址`,
      },
      toStation: {
        label: t`下车站点`,
      },
      toStationName: {
        label: t`下车站点名称`,
      },
      toStationAddress: {
        label: t`下车站点地址`,
      },
      writeOffDate: {
        label: t`上车时间`,
        text: DATETIME_TEXT,
      },
      state: {
        label: t`状态`,
        text: (v) => ENUMS.ticketState[v],
        //  0：未乘车，1：已乘车`,
      },
      passenger: {
        label: t`乘车人（平台用户username）`,
      },
      passengerName: {
        label: t`客户`,
        text: (_, record) => {
          const { passengerName, passengerPhone } = record;
          return `${passengerName} ${passengerPhone}`;
        },
      },
      passengerPhone: {
        label: t`乘车人电话`,
      },
    },
  ],
  accountRequestInfos: [
    {
      label: t`分账请求信息列表`,
      appId: {
        label: t`appId`,
      },
      id: {
        label: t`订单id`,
      },
      transactionId: {
        label: t`微信支付订单号`,
      },
      outOrderNo: {
        label: t`商户系统内部的分账单号`,
      },
      orderId: {
        label: t`微信分账单号,微信支付系统返回的唯一标识`,
      },
      state: {
        label: t`分账单状态（每个接收方的分账结果请查看receivers中的result字段）<br/> 1、PROCESSING：处理中<br/> 2、FINISHED：分账完成`,
      },
    },
  ],
  crBy: {
    label: t`创建者`,
  },
  upBy: {
    label: t`最后修改者`,
  },
  crDate: {
    label: t`创建时间`,
    text: DATETIME_TEXT,
  },
  upDate: {
    label: t`最后修收时间`,
  },
  tenantId: {
    label: t`租户ID`,
  },
  id: {
    label: t`任务编号`,
  },
  // 前端新增字段
  startArea: {
    label: t`上车站点`,
    text: (_, record) => {
      const { startMainAreaText = '', startStationName = '' } = record?.orderBaseModel || {};
      const text = `${startMainAreaText}-${startStationName}`;
      return text;
    },
  },
  endArea: {
    label: t`下车站点`,
    text: (_, record) => {
      const { endMainAreaText = '', endStationName = '' } = record?.orderBaseModel || {};
      const text = `${endMainAreaText}-${endStationName}`;
      return text;
    },
  },
  showPrice: {
    label: t`价格(公司/司机)`,
    text: (_, record) => {
      const { carrierPrice, driverServiceFee } = record;
      return t`${carrierPrice.toFixed(2)} , ${driverServiceFee.toFixed(2)}`;
    },
  },

  // 派车字段
  fullname: {
    label: t`司机`,
  },
  showCar: {
    label: t`承运车辆`,
    text: (_, record) => {
      const { mainland, hk } = record?.licenseNo;
      return `${mainland}(${hk})`;
    },
    ui_view: {
      renderer: (_, record) => {
        const { mainland, hk } = record?.licenseNo;
        return `${mainland}/${hk}`;
      },
      span: 2,
    },
  },
  // 变更字段
  arCarrierCompanyId: {
    label: t`公司id`,
  },
  arCarrierCompanyName: {
    label: t`公司名称`,
  },
  arCarrierCompanyPrice: {
    label: t`公司费用`,
    ui: {
      itemProps: {
        required: true,
      },
    },
  },
  showDriver: {
    label: t`承运司机`,
    ui_view: {
      renderer: (_, record) => {
        const { fullname, phone } = record;
        return `${fullname} ${phone}`;
      },
      span: 2,
    },
  },
  sendCarOrderPrice: {
    label: t`派单价格`,
    ui_view: {
      renderer: (_, record) => {
        const { carrierPrice } = record?.taskInfo;
        return `¥${carrierPrice}`;
      },
      span: 4,
    },
  },
  orderRemark: {
    label: t`订单备注`,
    ui_view: {
      renderer: (_, record) => {
        const { remarks } = record?.orderInfo?.ccOrder || {};
        return <span style={{ color: 'red' }}>{remarks || '无'}</span>;
      },
      span: 4,
    },
  },
  driverServiceFee: {
    label: t`司机费用`,
    ui_edit: {
      span: 4,
      itemProps: { required: true, format: 'number' },
    },
  },
};
